package com.duoketech.controller.web.activityorder;

import com.duoketech.config.Constant;
import com.duoketech.controller.BaseController;
import com.duoketech.controller.web.ICRUD;
import com.duoketech.exception.ValidateException;
import com.duoketech.model.activity.Activity;
import com.duoketech.model.activityorder.ActivityOrder;
import com.duoketech.service.web.activityorder.ActivityOrderService;
import com.duoketech.utils.PageUtil;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Page;

public class ActivityOrderController extends BaseController implements ICRUD {

    @Override
    public void listAll() {
        int pageNum = getParaToInt("pageNum",1);
        int pageSize = getParaToInt("pageSize",20);
        Kv kv = Kv.by(Constant.MEMBER_ID_KEY,getShopMemberId());
        Page<ActivityOrder> result = (Page<ActivityOrder>) ActivityOrderService.ME.pageAllRecord(getModel(ActivityOrder.class),pageNum,pageSize,kv);
        for (ActivityOrder o:result.getList()){
            o.set(ActivityOrder.STATUS,o.status());
            o.set(ActivityOrder.PRICE,PageUtil.fengToYuan(o.getInt(ActivityOrder.PRICE)));
        }
        renderObject(result);
    }

    @Override
    public void queryById() {
        int id = getParaToInt("id",0);
        try {
            ActivityOrder o = (ActivityOrder) ActivityOrderService.ME.queryBy(id,getModel(ActivityOrder.class),Kv.create());
            int objId = o.getInt(ActivityOrder.OBJ_ID);
            setAttr("activity",Activity.dao.findById(objId));
            setAttr("activityOrder",ActivityOrderService.ME.queryBy(id,getModel(ActivityOrder.class),Kv.create()));
            render("edit-"+viewModel()+".html");
            } catch (ValidateException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void doAdd() {
        try {
            ActivityOrderService.ME.doAdd(getModel(ActivityOrder.class),Kv.create());
            renderSuccess("操作成功");
            } catch (ValidateException e) {
            renderError(e.getMessage());
        }

    }

    @Override
    public void doUpdate() {
        try {
            ActivityOrderService.ME.update(getModel(ActivityOrder.class),Kv.create());
            renderSuccess("操作成功");
            } catch (ValidateException e) {
            renderError(e.getMessage());
        }
    }

    @Override
    public void doDelete() {
        int id = getParaToInt("id",0);
        try {
        ActivityOrderService.ME.deleteBy(id,getModel(ActivityOrder.class),Kv.create());
        renderSuccess("操作成功");
        } catch (ValidateException e) {
        renderError(e.getMessage());
        }
    }
  }